<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * User: gavin
 * Date: 15-3-25
 * Time: 下午6:24
 */
class Ghc_blog_model extends CI_Model
{

    public function __construct()
    {
        parent::__construct();
        //装载database
        $this->load->database();
    }

    /**
     * @param $title
     * @param $date
     * @param $content
     * @param $author
     * @param $sortid
     * @param string $type
     * @param int $views
     * @param string $top
     * @param string $hide
     * 通用插入
     */
    public function insert($title,$date,$content,$author,$sortid,$showimg=null,$type='blog',$views=0,$top='n',$hide='n')
    {
        $value['title'] = $title;
        $value['date'] = $date;
        $value['content'] = $content;
        $value['author'] = $author;
        $value['sortid'] = $sortid;
        $value['type'] = $type;
        $value['views'] = $views;
        $value['top'] = $top;
        $value['hide'] = $hide;
        $value['showimg'] = $showimg;
        $this->db->insert('ghc_blog',$value);
    }

    /**
     * @param $qid
     * @param $title
     * @param $date
     * @param $content
     * @param $author
     * @param $sortid
     * 在文章修改页面使用，通过qid来更新列表
     */
    public function update($qid,$title,$date,$content,$author,$sortid,$showimg=null)
    {
        $where['qid'] = $qid;
        $value['title'] = $title;
        $value['date'] = $date;
        $value['content'] = $content;
        $value['author'] = $author;
        $value['sortid'] = $sortid;
        $value['showimg'] = $showimg;
        $this->db->where($where);
        $this->db->update('ghc_blog',$value);
    }

    /**
     * @return mixed
     * 获取blog总数
     */
    public function getAllNums()
    {
        $sql = sprintf("select count(*) as nums from ghc_blog where hide='n'");
        $nums = $this->db->query($sql)->first_row()->nums;
        return $nums;
    }

    /**
     * @param $qid
     * @return mixed
     * 通过qid获取文章信息
     */
    public function getBlogById($qid)
    {
        $sql = sprintf("select * from ghc_blog where qid=%s limit 1",$qid);
        $query = $this->db->query($sql);
        return $query->first_row();
    }

    public function getBlogByDate($date)
    {
        $sql = sprintf("select * from ghc_blog where date=%s limit 1",$date);
        $query = $this->db->query($sql);
        return $query->first_row();
    }

    /**
     * @param $limit
     * @param $offset
     * @return mixed
     * 获取所有没有被隐藏的文章
     */
    public function listBlog($limit,$offset){
        $where['hide'] = 'n';
        $query = $this->db->where($where)->order_by('date','desc')->limit($limit,$offset)->get('ghc_blog');
        return $query->result();
    }

    /**
     * @param $qid
     * 通过qid隐藏文章/初期删除采用隐藏的手段
     */
    public function hideBlogById($qid)
    {
        $sql = sprintf("UPDATE ghc_blog SET hide='y'  WHERE qid=%s",$qid);
        $this->db->query($sql);
    }

    /**
     * @param $qid
     * 通过qid删除文章
     */
    public function deleteBlogById($qid)
    {
        $value['qid'] = $qid;
        $this->db->delete('ghc_blog',$value);
    }

    /**
     * @param $qid
     * 设置浏览数+1通过qid
     */
    public function addView($qid)
    {
        $sql = sprintf("UPDATE ghc_blog SET views=views+1  WHERE qid=%s",$qid);
        $this->db->query($sql);
    }

    public function addViewByDate($date)
    {
        $sql = sprintf("UPDATE ghc_blog SET views=views+1  WHERE date=%s",$date);
        $this->db->query($sql);
    }

    public function getViewNums()
    {
        $sql = sprintf("select sum(views) as nums from ghc_blog where hide='n'");
        $query = $this->db->query($sql);
        return $query->first_row()->nums;
    }

    public function getAnyView()
    {
        $sql = sprintf("select sortid, sum(views) as views from ghc_blog where hide='n' group by sortid");
        $query = $this->db->query($sql);
        return $query->result();
    }

    public function getAnyBlog()
    {
        $sql = sprintf("select sortid, count(qid) as nums from ghc_blog where hide='n' group by sortid");
        $query = $this->db->query($sql);
        return $query->result();
    }

    /**
     * @param int $limit
     * @return mixed
     * 工作动态首页
     */
    public function getNewsBlog($limit=4)
    {
        $sql = sprintf("select * from ghc_blog where hide='n' and (sortid=2 or sortid=3 or sortid=4 or sortid=5)  ORDER BY date DESC limit %s",$limit);
        $query = $this->db->query($sql);
        return $query->result();
    }

    /**
     * @param int $limit
     * @return mixed
     * 通知公告首页
     */
    public function getTips($limit=4)
    {
        $sql = sprintf("select * from ghc_blog where hide='n' and sortid=1  ORDER BY date DESC limit %s",$limit);
        $query = $this->db->query($sql);
        return $query->result();
    }

    /**
     * @param int $sortid
     * @return mixed
     * 获取一个分类的blog数量
     */
    public function getNumsBySortid($sortid=1)
    {
        $sql = sprintf("select count(*) as nums from ghc_blog where hide='n' and sortid=%s",$sortid);
        $query = $this->db->query($sql);
        return $query->first_row()->nums;
    }

    public function listBlogBySortid($limit,$offset,$sortid){
        $where['hide'] = 'n';
        $where['sortid'] = $sortid;
        $query = $this->db->where($where)->order_by('date','desc')->limit($limit,$offset)->get('ghc_blog');
        return $query->result();
    }
}